﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using FineUI;
using SDLITS.Model;
using SDLITS.Common;
using System.Data;
using System.Data.SqlClient;
using MySoft.Data;

namespace SDLITS.Web.Driver
{
    public partial class CardRevenueManage : CommonBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DrpBind();
                txtEndDate.SelectedDate = DateTime.Now;
            }
        }

        public void DrpBind()
        {
            List<UserInRole> roleList = SDDbSession.Default.From<UserInRole>().Where(UserInRole._.RoleID == 25 || UserInRole._.RoleID == 29).ToList();
            List<int> userList = new List<int>();
            foreach (var item in roleList)
            {
                userList.Add(item.UserID.Value);
            }
            List<User> user = SDDbSession.Default.From<User>().Where(SDLITS.Model.User._.UserID.SelectIn<int>(userList.ToArray())).ToList();
            cmbUsers.DataSource = user;
            cmbUsers.DataTextField = "RealName";
            cmbUsers.DataValueField = "UserID";
            cmbUsers.DataBind();

            cmbUsers.Items.Insert(0, new ListItem("--全体人员--", "0"));

        }

        public void DataBindToGridview()
        {
            DateTime? startDate = null;
            DateTime? endDate = null;
            WhereClip wc = WhereClip.All;
            if (!string.IsNullOrEmpty(txtStartDate.Text))
            {
                startDate = Convert.ToDateTime(txtStartDate.Text);
                wc = wc && V_CardRevenue._.CreateDate > txtStartDate.Text;
            }
            if (!string.IsNullOrEmpty(txtEndDate.Text))
            {
                endDate = Convert.ToDateTime(txtEndDate.Text).AddDays(1);
                wc = wc && V_CardRevenue._.CreateDate < Convert.ToDateTime(txtEndDate.Text).AddDays(1);
            }
            int createUserID = -1;
            if (cmbUsers.SelectedValue != "0")
            {
                createUserID = Convert.ToInt32(cmbUsers.SelectedValue);
                wc = wc && V_CardRevenue._.CreateUserID == createUserID;
            }

            Grid1.Hidden = true;
            if (cbl_all.Checked)
            {

                int recodeCount = SDDbSession.Default.From<V_CardRevenue>().Where(wc).Count();
                Grid1.RecordCount = recodeCount;
                List<V_CardRevenue> cardList = SDDbSession.Default.From<V_CardRevenue>().Where(wc).Page(Grid1.PageSize, (Grid1.PageIndex + 1)).OrderBy(V_CardRevenue._.CreateDate.Desc).ToList();
                Grid1.DataSource = cardList;
                Grid1.DataBind();
                Grid1.Hidden = false;
                //SqlParameter[] sp = new SqlParameter[]{
                //    new SqlParameter("@CreateUserID",createUserID),
                //    new SqlParameter("@MemcardNo",txtMemCardNo.Text.Trim()),
                //    new SqlParameter("@StartDate",startDate),
                //    new SqlParameter("@EndDate",endDate),
                //    new SqlParameter("@SPAction","SelectByDateRange")
                //};
                //DataSet ds = SDDbSession.Default.FromProc("TableCardRevenue").AddParameter(sp).ToDataSet();
                //if (ds != null && ds.Tables.Count > 0)
                //{
                //    Grid1.Hidden = false;
                //    Grid1.DataSource = ds.Tables[0];
                //    Grid1.DataBind();
                //}
            }
            SqlParameter[] sp2 = new SqlParameter[]{
                    new SqlParameter("@CreateUserID",createUserID),
                    new SqlParameter("@MemcardNo",txtMemCardNo.Text.Trim()),
                    new SqlParameter("@StartDate",startDate),
                    new SqlParameter("@EndDate",endDate),
                    new SqlParameter("@SPAction","SelectTotalByDateRange")
                };
            DataSet ds2 = SDDbSession.Default.FromProc("TableCardRevenue").AddParameter(sp2).ToDataSet();
            if (ds2 != null && ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds2.Tables[0].Rows[0];
                lblInitialAmount.Text = dr["InitialAmount"] is DBNull ? "" : dr["InitialAmount"].ToString();
                if (string.IsNullOrEmpty(lblInitialAmount.Text))
                {
                    lblInitialAmount.Text = "0";
                }

                lblWorkAmount.Text = dr["WorkAmount"] is DBNull ? "" : dr["WorkAmount"].ToString();
                if (string.IsNullOrEmpty(lblWorkAmount.Text))
                {
                    lblWorkAmount.Text = "0";
                }

                lblBonusAmount.Text = dr["BonusAmount"] is DBNull ? "" : dr["BonusAmount"].ToString();
                if (string.IsNullOrEmpty(lblBonusAmount.Text))
                {
                    lblBonusAmount.Text = "0";
                }

                lblTotal.Text = dr["TotalAmount"] is DBNull ? "" : dr["TotalAmount"].ToString();
                if (string.IsNullOrEmpty(lblTotal.Text))
                {
                    lblTotal.Text = "0";
                }

                lblTotalAll.Text =
                    (Convert.ToDecimal(lblInitialAmount.Text) + Convert.ToDecimal(lblWorkAmount.Text) +
                     Convert.ToDecimal(lblBonusAmount.Text) +
                     Convert.ToDecimal(lblTotal.Text)).ToString();

                lblDriverSum.Text = ds2.Tables[1].Rows[0][0].ToString();
                lblCompanySum.Text = ds2.Tables[2].Rows[0][0].ToString();
                lblTempSum.Text = ds2.Tables[3].Rows[0][0].ToString();
            }
        }
        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
            DataBindToGridview();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            DataBindToGridview();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SetPrintSession();
            PageContext.RegisterStartupScript(Window1.GetShowReference("CardRevenuePrint.aspx", "打印办卡记录"));
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            SetPrintSession();
            if (!string.IsNullOrEmpty(txtMemCardNo.Text))
            {
                PageContext.RegisterStartupScript(Window1.GetShowReference("CardRevenuePrintPlus.aspx", "打印办卡记录"));
            }
            else
            {
                Alert.Show("只能为一张卡做补打印，请输入卡号查询。");
            }
        }

        protected void cmbUsers_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataBindToGridview();
        }
        private void SetPrintSession()
        {
            var mrp = new ModelCardRevenuePrint
            {
                StartDate = txtStartDate.Text,
                EndDate = txtEndDate.Text,
                MemCardNo = txtMemCardNo.Text,
                Workder = cmbUsers.Text,
                InitialAmount = lblInitialAmount.Text,
                WorkAmount = lblWorkAmount.Text,
                BonusAmount = lblBonusAmount.Text,
                Total = lblTotal.Text,
                TotalAll = lblTotalAll.Text
            };
            Session["CardRevenuePrint"] = mrp;
        }
    }
    public class ModelCardRevenuePrint
    {
        public string StartDate { get; set; }
        public string EndDate { get; set; }
        public string MemCardNo { get; set; }
        public string Workder { get; set; }
        public string InitialAmount { get; set; }
        public string WorkAmount { get; set; }
        public string BonusAmount { get; set; }
        public string Total { get; set; }
        public string TotalAll { get; set; }
    }
}